package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.pms.model.PmsCategory;
import com.macro.mall.tiny.modules.pms.service.PmsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-10-09
 */
@RestController
@Api(tags = "PmsCategoryController")
@Tag(name = "PmsCategoryController", description = "分类管理")
@RequestMapping("/pms/pmsCategory")
public class PmsCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;

    @ApiOperation(value = "前台获取所有分类")
    @GetMapping("/listAll")
    public CommonResult<List<PmsCategory>> listBrand() {
        return CommonResult.success(pmsCategoryService.list(new QueryWrapper<>(new PmsCategory()).isNotNull("icon")));
    }

    @ApiOperation(value = "前台获取树形分类")
    @GetMapping("/listAllAsTree")
    public CommonResult<List<PmsCategory>> listTree() {

        List<PmsCategory> list = pmsCategoryService.list();
        Map<Integer, PmsCategory> map = list.stream().collect(Collectors.toMap(PmsCategory::getId, category -> category));

        List<PmsCategory> tree = new ArrayList<>();
        for (PmsCategory category : list) {
            if (category.getParentId() == null || category.getParentId() == 0) {
                tree.add(category);
            } else {
                PmsCategory parent = map.get(category.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(category);
                }
            }
        }
        return CommonResult.success(tree);
    }

    @ApiOperation(value = "前台获取根据parentId获取子分类")
    @GetMapping("/listSon/{parentId}")
    public CommonResult<List<PmsCategory>> listTree(@ApiParam("父id")@PathVariable Integer parentId) {
        List<PmsCategory> list = pmsCategoryService.list(new QueryWrapper<PmsCategory>().eq("parent_id", parentId));
        return CommonResult.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "后台添加分类")
    public CommonResult<Void> addBrand(@RequestBody PmsCategory PmsCategory) {
        pmsCategoryService.save(PmsCategory);
        return CommonResult.success(null);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "后台删除分类")
    public CommonResult<Void>batchDelete(@RequestBody List<Integer> ids){
        pmsCategoryService.removeByIds(ids);
        return CommonResult.success(null);
    }

    @PutMapping("/update/{brandId}")
    @ApiOperation(value = "后台修改分类")
    public CommonResult<Void> updateBrand(@ApiParam("分类id") @PathVariable(value = "brandId") Integer brandId, @RequestBody PmsCategory PmsCategory) {
        PmsCategory.setId(brandId);
        pmsCategoryService.updateById(PmsCategory);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "后台分页获取分类")
    @PostMapping("/page")
    public CommonResult<IPage<PmsCategory>> listBrand(@RequestBody PmsCategory pmsCategory) {
        Page<PmsCategory> pageParam = new Page<>(pmsCategory.getPageNum(), pmsCategory.getPageSize(),Boolean.TRUE);
        QueryWrapper<PmsCategory> queryWrapper = new QueryWrapper<>();
        if (pmsCategory.getId() != null) {
            queryWrapper.eq("id", pmsCategory.getId());
        }
        if (pmsCategory.getParentId() != null) {
            queryWrapper.eq("parent_id", pmsCategory.getParentId());
        }
        if (pmsCategory.getName() != null && !pmsCategory.getName().isEmpty()) {
            queryWrapper.like("name", pmsCategory.getName());
        }
        if (pmsCategory.getShowStatus() != null) {
            queryWrapper.eq("show_status", pmsCategory.getShowStatus());
        }
        queryWrapper.orderByDesc("id").orderByAsc("sort");

        IPage<PmsCategory> result = pmsCategoryService.page(pageParam, queryWrapper);
        return CommonResult.success(result);
    }
}

